package com.hss.common.https;

import android.content.Context;
import android.text.TextUtils;

import org.apache.http.conn.ssl.SSLSocketFactory;

import java.io.IOException;
import java.io.InputStream;
import java.net.Socket;
import java.net.UnknownHostException;
import java.security.KeyManagementException;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.UnrecoverableKeyException;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;

import javax.net.ssl.KeyManagerFactory;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;

import org.apache.http.conn.ssl.SSLSocketFactory;


/**
 * @author 黄赛赛
 * @version V1.0
 * @Title: ${file_name}
 * @Package ${package_name}
 * @Description: ${todo}()
 * @date 2016/3/26 22:31
 */
public class MySSLSocketFactory extends SSLSocketFactory {
    SSLContext sslContext = SSLContext.getInstance("TLS");
//	SSLContext sslContext = SSLContext.getInstance("SSL");

    public MySSLSocketFactory(KeyStore truststore, String pwd)
            throws NoSuchAlgorithmException, KeyManagementException,
            KeyStoreException, UnrecoverableKeyException {
        super(truststore);

        TrustManager tm = new X509TrustManager() {
            @Override
            public X509Certificate[] getAcceptedIssuers() {
                return null;
            }

            @Override
            public void checkServerTrusted(X509Certificate[] chain,
                                           String authType) throws CertificateException {
            }

            @Override
            public void checkClientTrusted(X509Certificate[] chain,
                                           String authType) throws CertificateException {
            }
        };

        if (TextUtils.isEmpty(pwd)){
            sslContext.init(null, new TrustManager[] { tm }, null);

        }else {
            KeyManagerFactory kmf = KeyManagerFactory.getInstance("X509");
            kmf.init(truststore, pwd.toCharArray());

            sslContext.init(kmf.getKeyManagers(), new TrustManager[] { tm },
                    null);
        }
        setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);

    }

    @Override
    public Socket createSocket() throws IOException {
        return sslContext.getSocketFactory().createSocket();
    }

    @Override
    public Socket createSocket(Socket socket, String host, int port,
                               boolean autoClose) throws IOException, UnknownHostException {

        return sslContext.getSocketFactory().createSocket(socket, host,
                port, autoClose);
    }


    public static  KeyStore getKeyStore(Context mContext, int keyStoreRawId, String pwd){
        KeyStore trustStore = null;
        try {
//            trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
            trustStore = KeyStore.getInstance("BKS");

            InputStream bksIs = null;
            bksIs = mContext.getResources().openRawResource(keyStoreRawId);
            trustStore.load(bksIs, pwd.toCharArray());
        } catch (KeyStoreException e) {
            e.printStackTrace();
        } catch (CertificateException e) {
            e.printStackTrace();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return trustStore;
    }
}
